// 奥数知识点总索引
// 统一管理小学和初中的所有知识点

import { EXTENDED_TOPICS } from './extended-topics';
import { MIDDLE_SCHOOL_TOPICS } from './middle-school-topics';

// 合并所有知识点
export const ALL_TOPICS = [...EXTENDED_TOPICS, ...MIDDLE_SCHOOL_TOPICS];

// 导出别名，兼容旧代码
export const TOPICS_INDEX = ALL_TOPICS;

// 按年级分类
export const TOPICS_BY_GRADE = {
  1: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(1)),
  2: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(2)),
  3: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(3)),
  4: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(4)),
  5: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(5)),
  6: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(6)),
  7: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(7)),
  8: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(8)),
  9: ALL_TOPICS.filter(topic => topic.gradeLevel.includes(9)),
};

// 按学段分类
export const TOPICS_BY_SCHOOL_LEVEL = {
  primary: ALL_TOPICS.filter(topic => 
    topic.gradeLevel.some(grade => grade <= 5)
  ),
  middle: ALL_TOPICS.filter(topic => 
    topic.gradeLevel.some(grade => grade >= 6)
  ),
  crossGrade: ALL_TOPICS.filter(topic => 
    topic.gradeLevel.some(grade => grade <= 5) && 
    topic.gradeLevel.some(grade => grade >= 6)
  )
};

// 按分类整理
export const TOPICS_BY_CATEGORY = {
  // 小学分类
  '基础计算': ALL_TOPICS.filter(topic => topic.category === '基础计算'),
  '行程问题': ALL_TOPICS.filter(topic => topic.category === '行程问题'),
  '几何图形': ALL_TOPICS.filter(topic => topic.category === '几何图形'),
  '数论基础': ALL_TOPICS.filter(topic => topic.category === '数论基础'),
  '逻辑推理': ALL_TOPICS.filter(topic => topic.category === '逻辑推理'),
  '应用问题': ALL_TOPICS.filter(topic => topic.category === '应用问题'),
  '组合计数': ALL_TOPICS.filter(topic => topic.category === '组合计数'),
  '高级思维': ALL_TOPICS.filter(topic => topic.category === '高级思维'),
  
  // 初中分类
  '代数基础': ALL_TOPICS.filter(topic => topic.category === '代数基础'),
  '函数': ALL_TOPICS.filter(topic => topic.category === '函数'),
  '几何基础': ALL_TOPICS.filter(topic => topic.category === '几何基础'),
  '数论进阶': ALL_TOPICS.filter(topic => topic.category === '数论进阶'),
  '组合数学': ALL_TOPICS.filter(topic => topic.category === '组合数学'),
  '概率统计': ALL_TOPICS.filter(topic => topic.category === '概率统计'),
};

// 按难度分类
export const TOPICS_BY_DIFFICULTY = {
  1: ALL_TOPICS.filter(topic => topic.difficulty === 1),
  2: ALL_TOPICS.filter(topic => topic.difficulty === 2),
  3: ALL_TOPICS.filter(topic => topic.difficulty === 3),
  4: ALL_TOPICS.filter(topic => topic.difficulty === 4),
  5: ALL_TOPICS.filter(topic => topic.difficulty === 5),
};

// 知识点统计
export const TOPICS_STATISTICS = {
  total: ALL_TOPICS.length,
  primary: TOPICS_BY_SCHOOL_LEVEL.primary.length,
  middle: TOPICS_BY_SCHOOL_LEVEL.middle.length,
  crossGrade: TOPICS_BY_SCHOOL_LEVEL.crossGrade.length,
  
  byGrade: {
    1: TOPICS_BY_GRADE[1].length,
    2: TOPICS_BY_GRADE[2].length,
    3: TOPICS_BY_GRADE[3].length,
    4: TOPICS_BY_GRADE[4].length,
    5: TOPICS_BY_GRADE[5].length,
    6: TOPICS_BY_GRADE[6].length,
    7: TOPICS_BY_GRADE[7].length,
    8: TOPICS_BY_GRADE[8].length,
    9: TOPICS_BY_GRADE[9].length,
  },
  
  byDifficulty: {
    1: TOPICS_BY_DIFFICULTY[1].length,
    2: TOPICS_BY_DIFFICULTY[2].length,
    3: TOPICS_BY_DIFFICULTY[3].length,
    4: TOPICS_BY_DIFFICULTY[4].length,
    5: TOPICS_BY_DIFFICULTY[5].length,
  },
  
  byCategory: Object.keys(TOPICS_BY_CATEGORY).reduce((acc, category) => {
    const map = TOPICS_BY_CATEGORY as unknown as Record<string, any[]>;
    acc[category] = (map[category] || []).length;
    return acc;
  }, {} as Record<string, number>)
};

// 搜索功能
export function searchTopics(query: string) {
  const lowerQuery = query.toLowerCase();
  return ALL_TOPICS.filter(topic => 
    topic.name.toLowerCase().includes(lowerQuery) ||
    topic.description.toLowerCase().includes(lowerQuery) ||
    topic.category.toLowerCase().includes(lowerQuery) ||
    topic.keyPoints.some(point => point.toLowerCase().includes(lowerQuery))
  );
}

// 根据年级获取推荐知识点
export function getRecommendedTopics(grade: number, difficulty?: number) {
  const byGrade = TOPICS_BY_GRADE as unknown as Record<number, any[]>;
  let topics = byGrade[grade] || [];
  
  if (difficulty) {
    topics = topics.filter(topic => topic.difficulty <= difficulty);
  }
  
  return topics.sort((a, b) => a.difficulty - b.difficulty);
}

// 获取前置知识点
export function getPrerequisites(topicId: string) {
  const topic = ALL_TOPICS.find(t => t.id === topicId);
  if (!topic || !topic.prerequisites) return [];
  
  return topic.prerequisites.map(prereqId => 
    ALL_TOPICS.find(t => t.id === prereqId)
  ).filter(Boolean);
}

// 获取后续知识点
export function getFollowUpTopics(topicId: string) {
  return ALL_TOPICS.filter(topic => 
    topic.prerequisites?.includes(topicId)
  );
}

// 学习路径推荐
export function generateLearningPath(startGrade: number, endGrade: number) {
  const path = [];
  
  for (let grade = startGrade; grade <= endGrade; grade++) {
    const gradeTopics = getRecommendedTopics(grade);
    path.push({
      grade,
      topics: gradeTopics,
      count: gradeTopics.length
    });
  }
  
  return path;
}

// 导出所有分类常量
export const ALL_CATEGORIES = [
  // 小学分类
  '基础计算', '行程问题', '几何图形', '数论基础', 
  '逻辑推理', '应用问题', '组合计数', '高级思维',
  // 初中分类  
  '代数基础', '函数', '几何基础', '数论进阶', 
  '组合数学', '概率统计'
];

export const DIFFICULTY_LABELS = {
  1: '入门',
  2: '基础', 
  3: '进阶',
  4: '高级',
  5: '竞赛'
};

export const GRADE_LABELS = {
  1: '一年级', 2: '二年级', 3: '三年级', 4: '四年级', 5: '五年级',
  6: '六年级', 7: '七年级', 8: '八年级', 9: '九年级'
};

// 完整的知识点体系概览
export const KNOWLEDGE_SYSTEM_OVERVIEW = {
  title: '奥数学院知识点体系',
  totalTopics: ALL_TOPICS.length,
  description: '涵盖小学1年级到初中9年级的完整奥数知识体系',
  
  primarySchool: {
    title: '小学奥数（1-5年级）',
    topics: TOPICS_BY_SCHOOL_LEVEL.primary.length,
    focus: '基础计算、逻辑推理、几何入门',
    categories: ['基础计算', '行程问题', '几何图形', '数论基础', '逻辑推理', '应用问题', '组合计数', '高级思维']
  },
  
  middleSchool: {
    title: '初中奥数（6-9年级）', 
    topics: TOPICS_BY_SCHOOL_LEVEL.middle.length,
    focus: '代数运算、函数概念、几何证明',
    categories: ['代数基础', '函数', '几何基础', '数论进阶', '组合数学', '概率统计']
  },
  
  crossGrade: {
    title: '跨年级知识点',
    topics: TOPICS_BY_SCHOOL_LEVEL.crossGrade.length,
    focus: '小学到初中的平滑过渡',
    examples: ['年龄问题', '平均数问题', '流水行船', '浓度问题', '利润问题']
  }
};
